#include <iostream>

using namespace std;

const int N = 510, INF = 0x3f3f3f3f; 
int a[N][N], f[N][N];
int n, m;

int main()
{
	cin >> n >> m; 
	for(int i = 0; i <= n; i++)
	{
		for(int j = 0; j <= m; j++)
		{
			if(i == 0 || j == 0) a[i][j] = INF;
			else cin >> a[i][j];
		}
	}
	a[0][1] = 0;
	for(int i = 0; i <= n; i++)
	{
		for(int j = 0; j <= m; j++)
		{
			f[i][j] = min(f[i - 1][j], f[i][j - 1]) + a[i][j];
		}
	}
	
	cout << f[n][m] << endl;
	
	return 0;
}

